<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
    <base th:href="${#httpServletRequest.getContextPath()}">
    <!-- 引入 layui.css -->
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" href="../../static/layui/css/layui.css">
    <!-- 引入 layui.js -->
    <script th:src="@{/layui/layui.js}" src="../../static/layui/layui.js"></script>
</head>
<body style="width: 99%">

<table class="layui-hide" id="test" lay-filter="test"></table>
</body>
<script type="text/html" id="toolbarDemo">
    <div class="demoTable">
        <div class="layui-inline">
            <div style="display: inline-block;">
                <label for="search_id"></label>
                <input class="layui-input" style="width: 150px" placeholder="学号" id="search_id"
                       autocomplete="off">
            </div>
            <div style="display: inline-block;">
                <label for="search_name"></label>
                <input class="layui-input" style="width: 150px" placeholder="姓名" id="search_name"
                       autocomplete="off">
            </div>
            <div class="layui-input-inline">
                <select name="政治面貌" id="search_politicalStatus">
                    <option value="">政治面貌</option>
                    <option value="群众">群众</option>
                    <option value="团员" class="layui-select-tips">团员</option>
                    <option value="预备党员" class="layui-select-tips">预备党员</option>
                    <option value="党员" class="layui-select-tips">党员</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block" id="search_gender">
                    <input type="radio" name="gender" value="男" title="男">
                    <input type="radio" name="gender" value="女" title="女">
                </div>
            </div>
        </div>
        <span>&nbsp;&nbsp;</span>
        <button class="layui-btn" id="search" data-type="reload">搜索</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="score">成绩</a>
    <a class="layui-btn layui-btn-xs" lay-event="modify">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script th:inline="none">
    layui.use('table', function () {
        const $ = layui.$;

        // 数据表单时间戳转换为日期显示
        function DateFormat(sjc) {
            const date = new Date(sjc);
            const y = date.getFullYear();
            let m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            let d = date.getDate();
            d = d < 10 ? ("0" + d) : d;
            return y + "-" + m + "-" + d;
        }

        const table = layui.table;
        table.render({
            id: 'table',
            elem: '#test'
            , url: '/student/get.json'
            , toolbar: '#toolbarDemo' // 开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { // 自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: '学号', width: 120, fixed: 'left', unresize: true, sort: true}
                , {field: 'name', title: '姓名', width: 120}
                , {field: 'gender', title: '性别', width: 80}
                , {
                    field: 'birthday',
                    title: '出生日期',
                    width: 120,
                    sort: true,
                    templet: function (res) {
                        return DateFormat(res.birthday);
                    }
                }
                , {field: 'hometown', title: '生源地', width: 120}
                , {field: 'departmentName', title: '院系', width: 150}
                , {field: 'politicalStatus', title: '政治面貌', width: 100}
                , {field: 'boarder', title: '是否住宿', width: 100}
                , {field: 'phone', title: '手机', width: 120}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180, unresize: true}
            ]]
            , page: true
        });

        // 头工具栏事件
        table.on('toolbar(test)', function (obj) {
            switch (obj.event) {
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这里什么都没有');
                    break;
            }
        });

        // 监听行工具事件
        table.on('tool(test)', function (obj) {
            const data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定要删除 ' + data.name + ' 吗?', function (index) {
                    $.ajax({
                        url: "/student/remove",
                        type: "post",
                        data: {
                            id: data.id
                        },
                        dataType: "json",
                        success: function (response) {
                            let code = response.code;
                            if (code == 0) {
                                layer.msg("删除成功!", {icon: 1, offset: "auto", time: 3000});
                                obj.del();
                                reload();
                                setTimeout(function () {
                                    let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                                    parent.layer.close(index);
                                }, 3000);
                            } else {
                                layer.msg("删除失败! " + response.code + " " + response.msg, {
                                    icon: 0,
                                    offset: "auto",
                                    time: 2000
                                });
                            }
                        },
                        error: function (response) {
                            layer.msg("删除失败! " + response.status + " " + response.statusText, {
                                icon: 0,
                                offset: "auto",
                                time: 2000
                            });
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'modify') {
                layer.open({
                    type: 2,
                    title: ['修改学生信息', 'font-size:25px;font-weight: bolder'],
                    area: ['660px', '550px'],
                    shadeClose: false,
                    content: '/student/' + data.id + '/modify/page.html'
                });
            } else if (obj.event === 'score') {
                layer.open({
                    type: 2,
                    title: ['成绩', 'font-size:25px;font-weight: bolder'],
                    area: ['800px', '400px'],
                    shadeClose: false,
                    content: '/student/' + data.id + '/course/results/page.html'
                });
            }
        });

        function reload() {
            //执行重载
            table.reload('table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    id: $("#search_id").val(),
                    name: $("#search_name").val(),
                    politicalStatus: $("#search_politicalStatus").val(),
                    gender: $("#search_gender [name='gender']:checked").val()
                }
            });
        }

        // 搜索
        $('body').on('click', '.demoTable .layui-btn', function () {
            reload();
        });
    })
</script>
</html>